package com.example.hzq.zqb.activity;

import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.example.hzq.zqb.R;
import com.example.hzq.zqb.fragment.Fragment1;
import com.example.hzq.zqb.fragment.Fragment2;
import com.example.hzq.zqb.fragment.Fragment3;
import com.example.hzq.zqb.fragment.Fragment4;
import com.example.hzq.zqb.fragment.Fragment5;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private LinearLayout layout1,layout2,layout3,layout4,layout5;
    private ImageView imageView1,imageView2,imageView3,imageView4,imageView5;
    private ViewPager vp;
    private List<Fragment> listfragment;
    private FragmentPagerAdapter fpadapter;
    public static boolean loginflag=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        initEvents();
        initFragment();
    }
    private void init() {
        layout1=(LinearLayout) findViewById(R.id.layout1);
        layout2=(LinearLayout)findViewById(R.id.layout2);
        layout3=(LinearLayout)findViewById(R.id.layout3);
        layout4=(LinearLayout)findViewById(R.id.layout4);
        layout5=(LinearLayout)findViewById(R.id.layout5);
        imageView1=(ImageView)findViewById(R.id.imageview1);
        imageView2=(ImageView)findViewById(R.id.imageview2);
        imageView3=(ImageView)findViewById(R.id.imageview3);
        imageView4=(ImageView)findViewById(R.id.imageview4);
        imageView5=(ImageView)findViewById(R.id.imageview5);
        vp=(ViewPager)findViewById(R.id.viewpage);
}
    View.OnClickListener onClickListener= new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            resetImage();
            switch (v.getId()) {
                case R.id.layout1:
                    selectTab(0);
                    break;
                case R.id.layout2:
                    selectTab(1);
                    break;
                case R.id.layout3:
                    selectTab(2);
                    break;
                case R.id.layout4:
                    selectTab(3);
                    break;
                case R.id.layout5:
                    selectTab(4);
                    break;
            }
        }
    };
    private void initEvents() {
        layout1.setOnClickListener(onClickListener);
        layout2.setOnClickListener(onClickListener);
        layout3.setOnClickListener(onClickListener);
        layout4.setOnClickListener(onClickListener);
        layout5.setOnClickListener(onClickListener);
    }
    private void initFragment() {
        listfragment = new ArrayList<Fragment>();
        listfragment.add(new Fragment1());
        listfragment.add(new Fragment2());
        listfragment.add(new Fragment3());
        listfragment.add(new Fragment4());
        listfragment.add(new Fragment5());
        fpadapter= new  FragmentPagerAdapter(getSupportFragmentManager())  {
            @Override
            public int getCount() {
                return listfragment.size();
            }

            @Override
            public Fragment getItem(int position) {
                return listfragment.get(position);
            }
        };
        vp.setAdapter(fpadapter);
        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }
            @Override
            public void onPageSelected(int position) {
                vp.setCurrentItem(position);
                resetImage();
                if (!loginflag&&position>1){
                    Intent intent=new Intent();
                    intent.setClass(MainActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                else
                selectTab(position);
            }
            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }
    private  void resetImage()
    {
       imageView1.setImageResource(R.mipmap.job);
       imageView2.setImageResource(R.mipmap.shop);
       imageView3.setImageResource(R.mipmap.money);
       imageView4.setImageResource(R.mipmap.news);
       imageView5.setImageResource(R.mipmap.mine);

    }
    private void selectTab(int i)
    {
        switch (i)
        {
            case 0:
                imageView1.setImageResource(R.mipmap.job_pre);
                break;
            case 1: imageView2.setImageResource(R.mipmap.shop_pre);
                break;
            case 2:
                imageView3.setImageResource(R.mipmap.money_pre);
                break;
            case 3:

                imageView4.setImageResource(R.mipmap.news_pre);
                break;
            case 4:

                imageView5.setImageResource(R.mipmap.mine_pre);
                break;
        }
        vp.setCurrentItem(i);
    }

    @Override
    protected void onResume() {
        super.onResume();
        int i=getIntent().getIntExtra("num",0);
        if (i!=0){
            resetImage();
            selectTab(1);
        }
    }
}
